<?php

class Banks extends HTech_Db_Table
{
    protected $_name    = 'view_banks';
    protected $_primary = 'BankId';
    
    protected $_dependentTables = array('Groups');
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Banks");
        $this->_gridMetadata['titleMods'] = array('LocationShortName','OfficerUsername');
        $this->_gridMetadata['onClick'] = 'admin.bank.add';
        $this->_gridMetadata['params'] = array('OfficerUserId', 'CommunityId', 'LocationId');
        $this->_gridMetadata['headers'] = array
            (
                array('BankId',           100, $this->translate("Code"),      'String'),
                array('OfficerUsername',   90, $this->translate("Officer"),   'String'),
                array('LocationShortName', 80, $this->translate("Branch"),    'String'),
                array('Community',        250, $this->translate("Community"), 'String'),
                array('Name',             250, $this->translate("Bank"),      'String'),
                array('President',        200, $this->translate("President"), 'String'),
                array('Treasurer',        200, $this->translate("Treasurer"), 'String'),
                array('Secretary',        200, $this->translate("Secretary"), 'String'),
                array('PresidentPhone',   120, $this->translate("P. phone"), 'String'),
                array('TreasurerPhone',   120, $this->translate("T. phone"), 'String'),
                array('SecretaryPhone',   120, $this->translate("S. phone"), 'String')
            );
        return $this;
    }
    
    protected function _setRecordDependentRows()
    {
        $this->_record->r = (object) array('groups' => null);
        $this->_record->r->groups = $this->_record->d->findDependentRowset('Groups');       
    }
    
    protected function _addCallback($id)
    {
        self::resetChildren($id);
        return $id;
    }   
    
    static function resetChildren($id)
    {     
        $banks = new Banks();
        $bankRecord = $banks->getRecord($id);
        $groups = new Groups();
        $c = 0;
        foreach($bankRecord->d->findDependentRowset('Groups') as $group) {
            $data = array('Name' => sprintf(Zend_Registry::get('HT_Config')->portfolio->group->name, $bankRecord->d->Name, $c + 1));
            $where = $groups->getAdapter()->quoteInto('GroupId = ?', $group->GroupId);
            $groups->update($data, $where);
            $c++;
        }
        Zend_Registry::get('db')->query('update view_clients set OfficerUserId = ? where BankId = ?', array($bankRecord->d->OfficerUserId, $id));
        Zend_Registry::get('db')->query('update view_clients set CommunityId = ? where BankId = ?', array($bankRecord->d->CommunityId, $id));
    }
    
    static function getActiveLoansCount($id)
    {
        return Zend_Registry::get('db')->fetchRow("select count(al.LoanId) hits from view_active_loans al, view_clients c 
            where c.BankId = ? and al.ClientId = c.ClientId",$id)->hits;
    }
}